<?php

/**
 * This is the model class for table "{{post}}".
 *
 * The followings are the available columns in table '{{post}}':
 * @property integer $id
 * @property string $title
 * @property string $url
 * @property string $thumbnail
 * @property string $summary
 * @property string $description
 * @property string $content
 * @property integer $category_id
 * @property integer $status
 * @property integer $language_id
 * @property integer $created_at
 * @property string $meta_description
 * @property string $meta_keywords
 * @property integer $count_view
 *
 * The followings are the available model relations:
 * @property Category $category
 */
class Post extends MyARContent
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Post the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{post}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, url, description, content', 'required'),
            array('title, url', 'unique', 'on' => 'unni'),
            array('category_id, count_view, type, status, created_at', 'numerical', 'integerOnly' => true),
            array('title, url', 'length', 'max' => 255),
            array('meta_description, meta_keywords, thumbnail', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, url, thumbnail, description, content, category_id, status, created_at, meta_description, meta_keywords, count_view', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.

        $relations = parent::relations();
        $relations['category'] = array(self::BELONGS_TO, 'Category', 'category_id');

        return $relations;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id' => 'ID',
            'title' => 'Title',
            'url' => 'Url',
            'thumbnail' => 'Thumbnail',
            'description' => 'Description',
            'content' => 'Content',
            'category_id' => 'Category',
            'status' => 'Status',
            'language_id' => 'Language',
            'created_at' => 'Created At',
            'meta_description' => 'Meta Desc',
            'meta_keywords' => 'Meta Keywords',
            'type' => 'Type',
            'count_view' => 'Count view',
        );
        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('post', $val);
        }

        return $array;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('thumbnail', $this->thumbnail, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('content', $this->content, true);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('status', $this->status);
        $criteria->compare('created_at', $this->created_at);
        $criteria->compare('count_view', $this->count_view);
        $criteria->compare('type', $this->type);
        $criteria->compare('meta_description', $this->meta_description, true);
        $criteria->compare('meta_keywords', $this->meta_keywords, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function getPublicUrl($absolute = false)
    {
        if ($absolute == FALSE)
        {
            return Yii::app()->createUrl("post/view", array(
                    'url' => $this->url,
            ));
        }
        else
        {
            return Yii::app()->createAbsoluteUrl("post/view", array(
                    'url' => $this->url,
            ));
        }
    }

    public function getPublicPhoto($absolute = false)
    {
        if ($absolute == FALSE)
        {
            return $this->resizePhoto($this->thumbnail, 160, 150);
        }
        else
        {
            return Yii::app()->createAbsoluteUrl($this->resizePhoto($this->thumbnail, 160, 150));
        }
    }

    public function getDateformat()
    {
        return $this->created_at ? date('d/m/Y', $this->created_at) : '';
    }

    public function getDayofWeek()
    {
        return $this->created_at ? MyUtil::dayVN($this->created_at) : '';
    }

    /**
     * Lấy các phần tử Bài viết thành một mảng
     * 
     */
    public static function getArrayPost()
    {
        $criteria = new CDbCriteria;
        $criteria->condition = "status > 0";
        $criteria->order = 'id DESC';
        $allPublic = self::model()->findAll($criteria);
        if ($allPublic != NULL)
        {
            foreach ($allPublic as $item)
            {
                $array[$item->id] = $item->title;
            }
        }
        return $array;
    }

    public static function getAllCatePost($cate_id, $id, $limit = 4)
    {
        $cri = new CDbCriteria();
        $cri->condition = 'status>0 and category_id=:cate and id!=:id';
        $cri->params = array(
            ':cate' => $cate_id,
            ':id' => $id,
        );
        $cri->limit = $limit;
        $cri->order = 'id DESC';
        return self::model()->findAll($cri);
    }

    public static function getAllPost($cate_id, $limit = 4)
    {
        $cri = new CDbCriteria();
        $cri->condition = 'status=:s and category_id=:cate';
        $cri->params = array(
            ':cate' => $cate_id,
            ':s' => Post::STATUS_PUBLIC,
        );
        $cri->limit = $limit;
        $cri->order = 'id DESC';
        return self::model()->findAll($cri);
    }

}
